﻿using System;
using System.Collections.Generic;
using System.Linq;

#if !WINRT
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Animation;
#else

using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;

#endif

namespace MishraReader.Windows.Controls
{
    public class AnimatedTabPanel : TabPanel
    {
        private bool _animationDone;

        internal void StartAnimation()
        {
            Dispatcher.BeginInvoke(new Action(AnimateItems));
        }

        private void AnimateItems()
        {
            if (_animationDone)
                return;

            _animationDone = true;

            var storyboard = (Storyboard)FindResource("MetroFadeSlideFast");
            var index = 0;
            foreach (TabItem item in Children)
            {
                if (item != null)
                {
                    var transform = new TranslateTransform();
                    var group = new TransformGroup
                                    {
                                        Children = {transform}
                                    };
                    item.RenderTransform = group;
                    var span = TimeSpan.FromMilliseconds(index*30);
                    index++;
                    var storyboard2 = storyboard.Clone();
                    storyboard2.BeginTime = span;
                    item.BeginStoryboard(storyboard2);
                }
            }
        }
    }
}